package cn.com.generaldata.os.ws.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.nutz.dao.Cnd;
import org.nutz.dao.impl.sql.SqlTemplate;
import org.nutz.ioc.annotation.InjectName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.com.generaldata.jsme_indicator_system.entity.ReportVersion;
import cn.com.generaldata.os.result.ReportVersionYearRangeResult;

import com.google.common.collect.Maps;

@InjectName
@Component
public class VersionWS {

	@Autowired
	private SqlTemplate indicatorSystemSqlTemplate;

	public ReportVersionYearRangeResult getReportVersionYearRange() {
		ReportVersionYearRangeResult result = new ReportVersionYearRangeResult();

		Date startYear = indicatorSystemSqlTemplate.queryForObject("SELECT MIN(start_date) FROM tbl_report_version",
				null, Date.class);
		Date endYear = indicatorSystemSqlTemplate.queryForObject("SELECT MAX(end_date) FROM tbl_report_version", null,
				Date.class);
		Calendar cal = Calendar.getInstance();
		cal.setTime(startYear);
		result.setStartYear(cal.get(Calendar.YEAR));
		cal.setTime(endYear);
		result.setEndYear(cal.get(Calendar.YEAR));
		return result;
	}

	/**
	 * 取得某个报表制度的版本列表.
	 * 
	 * @param systemId
	 *            制度ID
	 * @return
	 */
	public List<ReportVersion> getVersionList(int systemId) {
		return indicatorSystemSqlTemplate.dao().query(ReportVersion.class, Cnd.where("statSystem", "=", systemId));
	}

	public Integer getReportVersion(String stat, Date date) {
		String sql = "select rv.id from tbl_report_version rv where rv.stat_system=@stat and rv.start_date <=@date and rv.end_date>=@date";
		Map<String, Object> param = Maps.newHashMap();
		param.put("date", date);
		param.put("stat", stat);
		Integer versionId = indicatorSystemSqlTemplate.queryForObject(sql, param, Integer.class);
		return versionId;
	}

}